//
//  HomeViewController.m
//  govern
//
//  Created by LiangLigeng on 14-8-18.
//  Copyright (c) 2014年 LiangLigeng. All rights reserved.
//

#import "HomeViewController.h"
//#import "FocusCollectionCell.h"
#import "FocusInfoModel.h"
#import "UIImageView+WebCache.h"
#import "HttpService.h"
#import "JSONKit.h"
#import "AFHTTPRequestOperationManager.h"
#import "PicNewsInfoModel.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"首页";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
#ifdef __IPHONE_7_0
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
#endif
    //导航栏背景
    UIImage *navBack=[UIImage imageNamed:@"p2_top_bar.png"];
    [self.navigationController.navigationBar setBackgroundImage:navBack forBarMetrics:UIBarMetricsDefault];
    
    //加载图片新闻
    [self loadImageNews];
    
    //初始化
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kNavHeight, kDeviceWidth, kDeviceHeight-kNavHeight*2-kTabBarHeight-20) collectionViewLayout:flowLayout];
    _focusGridView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 140, ScreenWidth, 242) collectionViewLayout:flowLayout];
    _focusGridView.dataSource=self;
    _focusGridView.delegate=self;
    // 注册cell
    [_focusGridView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"GradientCell"];
    [self.view addSubview:_focusGridView];
    
}

-(void)loadImageNews
{
    NSArray *urls = @[[NSURL URLWithString:@"http://www.ghzw.cn/wzsq/UploadFiles_9194/201109/20110915154150869.bmp"],
                      [NSURL URLWithString:@"http://sudasuta.com/wp-content/uploads/2013/10/10143181686_375e063f2c_z.jpg"],
                      [NSURL URLWithString:@"http://www.yancheng.gov.cn/ztzl/zgycddhsdgy/xwdt/201109/W020110902584601289616.jpg"],
                      [NSURL URLWithString:@"http://fzone.oushinet.com/bbs/data/attachment/forum/201208/15/074140zsb6ko6hfhzrb40q.jpg"]];
    _imageURLs=[[NSMutableArray alloc] initWithCapacity:4];
    //[_imageURLs addObjectsFromArray:urls];
    _imagePlayerView =[[ImagePlayerView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 150)];
    //[_imagePlayerView initWithCount:_imageURLs.count delegate:self];
    _imagePlayerView.scrollInterval = 5.0f;
    
    // adjust pageControl position
    _imagePlayerView.pageControlPosition = ICPageControlPosition_BottomCenter;
    
    // hide pageControl or not
    _imagePlayerView.hidePageControl = NO;
    [self.view addSubview:_imagePlayerView];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://222.85.136.55:8033/api/getImageNews" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *images = (NSArray *)responseObject;
    //    NSDictionary *images=[jsonDictionary objectForKey:@"News"];
        [_imageURLs removeAllObjects];
        for (NSDictionary *image in images) {
            PicNewsInfoModel *pic=[PicNewsInfoModel picNewsFromDictionary:image];
            [PicNewsInfoModel savePicNews:pic];
            //NSString *imgurl=[image objectForKey:@"imgurl"];
            NSLog(@"imageurl:......%@",pic.imageUrl);
            
            [_imageURLs addObject:[NSURL URLWithString:pic.imageUrl]];
        }
        [_imagePlayerView initWithCount:_imageURLs.count delegate:self];
    //    NSLog(@"JSON: %@", images);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ImagePlayerViewDelegate
- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView loadImageForImageView:(UIImageView *)imageView index:(NSInteger)index
{
    [imageView setImageWithURL:[_imageURLs objectAtIndex:index] placeholderImage:nil];
    
    //imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[self.imageURLs objectAtIndex:index]]];
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView didTapAtIndex:(NSInteger)index
{
    NSLog(@"did tap index = %d", (int)index);
}

#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _focusArray.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"GradientCell";
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    FocusInfoModel *focus=_focusArray[indexPath.row];
    cell.backgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:focus.ico]];
    //cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];
    return cell;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _focusArray=[FocusInfoModel getAllFocus];
    [_focusGridView reloadData];
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(96, 100);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //cell.backgroundColor = [UIColor whiteColor];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
